package com.ht.controller;


import com.ht.config.IdWorker;
import com.ht.data.Result;
import com.ht.domain.bo.TbDiagnoseOrderBO;
import com.ht.domain.bo.TbScheduleBO;
import com.ht.domain.bo.UserBO;
import com.ht.domain.vo.TbDiagnoseOrderVO;
import com.ht.domain.vo.TbScheduleVO;
import com.ht.dto.TbDiagnoseOrderDTO;
import com.ht.dto.TbScheduleDTO;
import com.ht.em.ResultEnum;
import com.ht.feign.PublicOutpatientFeign;
import com.ht.service.DiagnoseOrderService;
import com.ht.service.ScheduleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController  //等于@Restquest + @Controller
@RequestMapping("/publicoutpatient")
@Api("普通们诊挂号管理系统")
@RefreshScope  //实时刷新
public class PublicOutpatientController {

    @Autowired
    private ScheduleService scheduleService;

    @Autowired
    private DiagnoseOrderService diagnoseOrderService;

    @Resource
    private PublicOutpatientFeign publicOutpatientFeign;

    @Autowired
    private IdWorker idWorker;



    @GetMapping("/queryDepartment")
    @ApiOperation("查询所有科室")
    @ApiImplicitParam()
    public Result queryDepartment(){
        Result all = publicOutpatientFeign.findDepartmentAll();
        return all;
    }



    /**
     *首页普通门诊跳转网上问诊-普通号页面
     * @return  普通门诊页面跳转成功的页面
     */
    @GetMapping("/query")
    @ApiOperation("跳转页面")
    @ApiImplicitParam()
    public String query(){
        System.out.println("普通门诊页面跳转成功");
        return "query";
    }

    /**
     *网上问诊-普通号页面(携带可挂号总人数)跳转到科室页面
     * @return     网上问诊-普通号页面跳转成功的页面
     */
    @GetMapping("/queryPublic")
    @ApiOperation("跳转页面")
    @ApiImplicitParam()
    public Result queryPublic(){
        Date date = new Date();
        System.out.println(date);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(date);
        System.out.println(format);
        TbScheduleBO bo = scheduleService.queryByDate(format);
        TbScheduleVO vo = new TbScheduleVO();
        BeanUtils.copyProperties(bo,vo);
        System.out.println("网上问诊-普通号页面跳转成功");
        return Result.OK().setData(vo);
    }


    /**
     * 根据科室查询当前这一天的排班表以及对应的值班医生
     * @param id   当前科室的id
     * @param format  需要查询的时间
     * @return
     */
    @GetMapping("/queryPublicById/{id}")
    @ApiOperation("查询排班表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "主键id",required = true,paramType = "path"),
            @ApiImplicitParam(name="format",value="访问时间",required = true,paramType = "query"),
    })
    public Result queryPublicById(@PathVariable("id") Long id,String format){

        List<TbScheduleBO> boList = scheduleService.queryPublicById(id,format);
        List<TbScheduleVO> voList = new ArrayList<>();
        int i= 0;
        for (TbScheduleBO bo : boList) {
            i += bo.getNum();
            TbScheduleVO vo = new TbScheduleVO();
            BeanUtils.copyProperties(bo,vo);
            voList.add(vo);
        }
        for (TbScheduleVO vo : voList) {
            vo.setRemainingNum(i);
        }
        return Result.OK().setData(voList);
    }

//    /**
//     * 立即挂号
//     * @param //doctorNanme  医生姓名
//     * @param //format  挂号看病的时间
//     * @param //status  挂号的场次
//     * @param //departmentName  挂号科室
//     * @return
//     */
//    @GetMapping("/registeredOne")
//    @ApiOperation("立即挂号")
////    @ApiImplicitParams({
////            @ApiImplicitParam(name="id",value = "主键id",required = true,paramType = "path"),
////            @ApiImplicitParam(name="format",value="访问时间",required = true,paramType = "query"),
////    })
//    public String registeredOne(TbDiagnoseOrderDTO dto){
//        TbDiagnoseOrderVO vo = new TbDiagnoseOrderVO();
//        BeanUtils.copyProperties(dto,vo);
//        return "下一面---立即挂号";
//    }
//
//    @GetMapping("/registeredTwo")
//    @ApiOperation("立即挂号")
////    @ApiImplicitParams({
////            @ApiImplicitParam(name="id",value = "主键id",required = true,paramType = "path"),
////            @ApiImplicitParam(name="format",value="访问时间",required = true,paramType = "query"),
////    })
//    public String registeredTwo(TbDiagnoseOrderDTO dto){
//        TbDiagnoseOrderVO vo = new TbDiagnoseOrderVO();
//        BeanUtils.copyProperties(dto,vo);
//        return "下一面---选择患者";
//    }
//
//
//    @GetMapping("/registeredThree")
//    @ApiOperation("选择患者,根据id查询数据")
////    @ApiImplicitParams({
////            @ApiImplicitParam(name="id",value = "主键id",required = true,paramType = "path"),
////            @ApiImplicitParam(name="format",value="访问时间",required = true,paramType = "query"),
////    })
//    public String registeredThree(TbDiagnoseOrderDTO dto){
//        TbDiagnoseOrderVO vo = new TbDiagnoseOrderVO();
//        BeanUtils.copyProperties(dto,vo);
//        return "下一面---选择患者";
//    }
//
//    @GetMapping("/registeredHour")
//    @ApiOperation("患者")
////    @ApiImplicitParams({
////            @ApiImplicitParam(name="id",value = "主键id",required = true,paramType = "path"),
////            @ApiImplicitParam(name="format",value="访问时间",required = true,paramType = "query"),
////    })
//    public String registeredHour(TbDiagnoseOrderDTO dto){
//        TbDiagnoseOrderVO vo = new TbDiagnoseOrderVO();
//        BeanUtils.copyProperties(dto,vo);
//        return "下一面---病症描述";
//    }
//
//    @GetMapping("/registeredFive")
//    @ApiOperation("病症描述")
////    @ApiImplicitParams({
////            @ApiImplicitParam(name="id",value = "主键id",required = true,paramType = "path"),
////            @ApiImplicitParam(name="format",value="访问时间",required = true,paramType = "query"),
////    })
//    public String registeredFive(TbDiagnoseOrderDTO dto){
//        TbDiagnoseOrderVO vo = new TbDiagnoseOrderVO();
//        BeanUtils.copyProperties(dto,vo);
//        return "下一面---过敏史";
//    }
//
//    @GetMapping("/registeredSix")
//    @ApiOperation("过敏史")
////    @ApiImplicitParams({
////            @ApiImplicitParam(name="id",value = "主键id",required = true,paramType = "path"),
////            @ApiImplicitParam(name="format",value="访问时间",required = true,paramType = "query"),
////    })
//    public String registeredSix(TbDiagnoseOrderDTO dto){
//        TbDiagnoseOrderVO vo = new TbDiagnoseOrderVO();
//        BeanUtils.copyProperties(dto,vo);
//        return "下一面---病程描述";
//    }

    @GetMapping("/registeredSeven")
    @ApiOperation("患者信息")
    @ApiImplicitParam(name="dto",value = "实体",required = true,paramType = "body")
    public Result registeredSeven(TbDiagnoseOrderDTO dto){
//        UserBO userBO = (UserBO) SecurityUtils.getSubject().getPrincipal();
//        if (userBO == null){
//            return Result.response(ResultEnum.NO_LOGIN);
//        }
        TbDiagnoseOrderVO vo = new TbDiagnoseOrderVO();
        BeanUtils.copyProperties(dto,vo);
        vo.setUserId(1);
        System.out.println("下一面---支付");
        return Result.OK().setData(vo);
    }

    /**
     *门诊挂号  -- 患者订单表增加,支付页面
     * @param dto  实体类
     * @return
     */

    @PostMapping("/outpatientRegistered")
    @ApiOperation("门诊挂号,患者订单表增加,支付页面")
    @ApiImplicitParam(name="dto",value = "实体",required = true,paramType = "body")
    public Result outpatientRegistered(TbDiagnoseOrderDTO dto){
//        UserBO userBO = (UserBO) SecurityUtils.getSubject().getPrincipal();
//        if (userBO == null){
//            return Result.response(ResultEnum.NO_LOGIN);
//        }
        TbDiagnoseOrderBO bo = new TbDiagnoseOrderBO();
        bo.setOrderType(dto.getOrderTypes());//订单类型
        bo.setDoctor(dto.getDoctorNanme());//预约医生
        bo.setPrice(dto.getPrice());//订单价格
        List list = new ArrayList();
        Map<String,String> map = new HashMap<>();
        map.put("身高",dto.getHeight() + "cm");
        map.put("体重",dto.getWeight() + "kg");
        map.put("过敏史",dto.getAllergyDetails());
        list.add(map);
        bo.setMessage(list.toString());//患者基本信息
        bo.setDescribe(dto.getDescribe());//病情描述
        bo.setCreateTime(new Date());//订单创建时间
        long noId = idWorker.nextId();
        bo.setOrderReference(noId);//订单编号
        bo.setPatientId(dto.getPatientId()); //患者id
        bo.setUserId(2);//用户id
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String s = sdf.format(new Date());
        bo.setEffectTime(s);//预定生效时间
        bo.setStatus("1");//订单状态
        diagnoseOrderService.outpatientRegistered(bo);
        System.out.println("连接医院挂号接口");
        return Result.OK();
    }














}

